package com.toro.dao.Impl;

import com.toro.dao.UserDao;
import com.toro.utils.JDBCUtils;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


/**
 * @ClassName: UserDaoImpl
 * @Description:
 * @Author: ToroLHZ
 * @Date: 2023/12/17 15:26
 */
@Slf4j
public class UserDaoImpl implements UserDao {
    /**
     * @Description: 通过用户名获取用户id
     * @Param: [username]
     * @Return: int
     * @Author: ToroLHZ
     * @Date: 2023/12/24 14:09
     **/
    @Override
    public int getUserIdByUsername(String username) {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select userid from t_user where username = ?";
        try {
            connection = JDBCUtils.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setString(1, username);
//            log.debug("组装后的sql:{}", ps);
            rs = ps.executeQuery();
            if (rs.next()) {
                int id = rs.getInt("userid");
                return id;
            } else {
                return 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            JDBCUtils.closeResource(connection, ps, rs);
        }
        return 0;
    }

    @Override
    public boolean getUser(String username, String password) {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select userid from t_user where username = ? and password = ?";
        try {
            connection = JDBCUtils.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
//            log.debug("组装后的sql:{}", ps);
            rs = ps.executeQuery();
            if (rs.next()) {
                int id = rs.getInt("userid");
                return id > 0;
            } else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            JDBCUtils.closeResource(connection, ps, rs);
        }
        return false;

    }

    /**
     * @Description: 通过用户id获取用户名
     * @Param: [userId]
     * @Return: java.lang.String
     * @Author: ToroLHZ
     * @Date: 2023/12/24 16:46
     **/
    @Override
    public String getUserNameByUserId(int userId) {
        Connection connection = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = "select username from t_user where userid = ?";
        try {
            connection = JDBCUtils.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setInt(1, userId);
//            log.debug("组装后的sql:{}", ps);
            rs = ps.executeQuery();
            if (rs.next()) {
                String username = rs.getString("username");
                return username;
            } else {
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            JDBCUtils.closeResource(connection, ps, rs);
        }
        return null;
    }
}
